const app = getApp()
const util = require("../../utils/utils.js")
// pages/order-detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isBuying: null,
    buyingId: null,
    order: {},
    dealing: {},
    collapseActiveNames: ['1'],
    sellerNickname: null,
    sellerId: null,
    buyerNickname: null,
    buyerId: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "订单详情"
    });
    var that = this
    this.setData({
      isBuying: app.globalData.isBuying,
      buyingId: app.globalData.viewBuyingId
    })
    var buyingId = that.data.buyingId
    wx.request({
      url: 'http://localhost:8888/buying/getDealingByBuying',
      method: 'GET',
      data: {
        id: buyingId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data.data);
        var photo = "dealing.photo"
        that.setData({
          dealing: res.data.data,
        })
        that.setData({
          [photo]: util.strToArr(that.data.dealing.photo)[0],
        })
        wx.request({
          url: 'http://localhost:8888/account/getAccount',
          method: 'GET',
          data: {
            id: res.data.data.username
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            that.setData({
              sellerNickname: res.data.data.username,
              sellerId: res.data.data.id
            })
          }
        })
      }
    })
    wx.request({
      url: 'http://localhost:8888/buying/getBuyingById',
      method: 'GET',
      data: {
        id: buyingId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          order: res.data.data
        })
        wx.request({
          url: 'http://localhost:8888/account/getAccount',
          method: 'GET',
          data: {
            id: res.data.data.username
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            that.setData({
              buyerNickname: res.data.data.username,
              buyerId: res.data.data.id
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onCollapseChange: function (event) {
    this.setData({
      collapseActiveNames: event.detail,
    });
  },
  contactSeller() {
    var user = this.data.sellerId
    wx.request({
      url: 'http://localhost:8888/chatting/getChatting',
      method: 'GET',
      data: {
        user1: app.globalData.loginAccount,
        user2: user
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.data == null) {
          wx.request({
            url: 'http://localhost:8888/chatting/insertChatting',
            method: 'POST',
            data: {
              user1: app.globalData.loginAccount,
              user2: user
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              app.globalData.chatWho = user
              wx.navigateTo({
                url: '../chat-detail/detail',
              })
            }
          })
        } else {
          if (res.data.data.user1 == app.globalData.loginAccount) {
            app.globalData.chatWho = res.data.data.user2
          }
          if (res.data.data.user2 == app.globalData.loginAccount) {
            app.globalData.chatWho = res.data.data.user1
          }
          wx.navigateTo({
            url: '../chat-detail/detail',
          })
        }
      }
    })
  },
  contactBuyer() {
    var user = this.data.buyerId
    wx.request({
      url: 'http://localhost:8888/chatting/getChatting',
      method: 'GET',
      data: {
        user1: app.globalData.loginAccount,
        user2: user
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.data == null) {
          wx.request({
            url: 'http://localhost:8888/chatting/insertChatting',
            method: 'POST',
            data: {
              user1: app.globalData.loginAccount,
              user2: user
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              app.globalData.chatWho = user
              wx.navigateTo({
                url: '../chat-detail/detail',
              })
            }
          })
        } else {
          if (res.data.data.user1 == app.globalData.loginAccount) {
            app.globalData.chatWho = res.data.data.user2
          }
          if (res.data.data.user2 == app.globalData.loginAccount) {
            app.globalData.chatWho = res.data.data.user1
          }
          wx.navigateTo({
            url: '../chat-detail/detail',
          })
        }
      }
    })
  }
})